<?php
namespace app\index\controller;

use app\index\model\AdminNavModel;
use think\Controller;
use think\Validate;
use app\index\model\AuthRuleModel;
use app\index\model\AuthGroupModel;
use app\index\model\PoliceStationModel;
use app\index\model\AuthGroupAccessModel;
use app\common\controller\BaseController;

class PoliceStation extends BaseController
{
    public function searchInfo(){
        return $this->authAjaxReturn(200,null,"success");
    }

    public function save(){
        $province = input("post.province");
        $city = input("post.city");
        $region = input("post.region");
        $policeStation = input("post.policeStation");
        $data = [
            "province" => $province,
            "city" => $city,
            "region" => $region,
            "policeStation" => $policeStation,
        ];
        $rule = [
            "province" => 'require|chs',
            "city" => 'require|chs',
            "region" => 'require|chs',
            "policeStation" => 'require|chs',
        ];
        $msg = [
            'province.require' => '省名不能为空',
            'province.chs'   => '省名只能为汉字',
            'city.require' => '市名不能为空',
            'city.chs'   => '市名只能为汉字',
            'region.require' => '区/县名不能为空',
            'region.chs'   => '区/县名只能为汉字',
            'policeStation.require' => '派出所名不能为空',
            'policeStation.chs'   => '派出所名只能为汉字',

        ];
        $validate = new Validate($rule,$msg);
        if(!$validate->check($data)){
            return $this->ajaxReturn(600,null,$validate->getError());
        }
        $model = new PoliceStationModel();
        $model->province = $province;
        $model->city = $city;
        $model->region = $region;
        $model->policeStation =$policeStation;
        $model->create_time = date("Y-m-d H:i:s");
        $data = $model
            ->where("policeStation",$policeStation)
            ->select();
        if($data){
            return $this->ajaxReturn(400,$data,"派出所已存在");
        }else{
            $model->save();
            return $this->ajaxReturn(200,null,"添加成功");
        }
    }

    public function update(){
        $province = input("post.province");
        $city = input("post.city");
        $region = input("post.region");
        $policeStation = input("post.policeStation");
        $data = [
            "province" => $province,
            "city" => $city,
            "region" => $region,
            "policeStation" => $policeStation,
        ];
        $rule = [
            "province" => 'require|chs',
            "city" => 'require|chs',
            "region" => 'require|chs',
            "policeStation" => 'require|chs',
        ];
        $msg = [
            'province.require' => '省名不能为空',
            'province.chs'   => '省名只能为汉字',
            'city.require' => '市名不能为空',
            'city.chs'   => '市名只能为汉字',
            'region.require' => '区/县名不能为空',
            'region.chs'   => '区/县名只能为汉字',
            'policeStation.require' => '派出所名不能为空',
            'policeStation.chs'   => '派出所名只能为汉字',

        ];
        $validate = new Validate($rule,$msg);
        if(!$validate->check($data)){
            return $this->ajaxReturn(600,null,$validate->getError());
        }
        $model = new PoliceStationModel();
        $model->province = $province;
        $model->city = $city;
        $model->region = $region;
        $model->policeStation =$policeStation;
        $model->create_time = date("Y-m-d H:i:s");
        $data = $model->where("region",$region)
            ->update(['policeStation'=>$policeStation]);
            return $this->ajaxReturn(200,$data,"修改成功");
    }

    public function query(){
        $page = input("get.page",1);
        $limit = input("get.limit",5);
        $query= input("get.query",'');
        $model = new PoliceStationModel();

        $map['province'] = ["like",'%'.$query.'%'];
        $map['city'] = ["like",'%'.$query.'%'];
        $map['region'] = ["like",'%'.$query.'%'];
        $data = $model
            ->alias('a')
            ->page($page,$limit)
            ->order('a.create_time desc')
            ->whereOr($map)->select();
        if ($data){
            return $this->ajaxReturn(200,$data,'存在你查找的信息');
        }else{
            return $this->ajaxReturn(400,$data,'不存在你查找的信息');
        }
    }

    public function del($policeStation){
        $model = new PoliceStationModel();
        $data = $model->where('policeStation',$policeStation)->delete();
        return $this->ajaxReturn(200,$data,'删除成功');
    }

    public function getPageLimit(){
        $model = new PoliceStationModel();
        $data = $model->count();
        return $this->ajaxReturn(200,$data,'刷新成功');

    }

    //获取条件查询管理员信息总数路由
    public function getQueryPageLimit(){
        $query = input("get.query");
        $model = new PoliceStationModel();
        $map['province'] = ["like",'%'.$query.'%'];
        $map['region'] = ["like",'%'.$query.'%'];
        $map['city'] = ["like",'%'.$query.'%'];
        $data = $model->whereOr($map)->count();
        return $this->ajaxReturn(200,$data,'获取成功');
    }

}